package cn.com.inossem.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.inossem.base.RequestActivity;
import cn.com.inossem.http.Api;
import cn.com.inossem.http.NetworkConnectGet;
import cn.com.inossem.model.ProjectBean;
import cn.com.inossem.utils.ToastUtil;
import cn.com.inossem.utils.Utils;

import static cn.com.inossem.activity.R.id.pro_period;

/**
 * 我的项目信息
 */
public class ProjectDetailActivity extends RequestActivity {
	private static String periodHtml = "周期<font color='#FDB118'> 0 </font>个月";
	private static String periodHtml2 = "<font color='#FDB118'> 0 </font>";
	private static String paymentHtml = "RMB<font color='#FDB118'><big><big> 0 </big></big></font>/天";
	private static String settleHtml = "RMB<font color='#FDB118'><big><big> 0 </big></big></font>元";
	private String proID;
	@BindView(R.id.common_title)
	TextView commonTitle;
	@BindView(R.id.common_right_img)
	ImageView commonRightImg;
	@BindView(R.id.pro_title)
	TextView proTitle;
	@BindView(R.id.project_detail_bottom1)
	LinearLayout projectDetailBottom1;
	@BindView(R.id.pro_demand_num)
	TextView proDemandNum;
	@BindView(R.id.pro_client_name)
	TextView proClientName;
	@BindView(R.id.pro_name)
	TextView proName;
	@BindView(R.id.pro_start_time)
	TextView proStartTime;
	@BindView(pro_period)
	TextView proPeriod;
	@BindView(R.id.pro_position)
	TextView proPosition;
	@BindView(R.id.pro_pay_way)
	TextView proPayWay;
	@BindView(R.id.pro_work_way)
	TextView proWorkWay;
	@BindView(R.id.pro_money_divider)
	View proMoneyDivider;
	@BindView(R.id.pro_my_payment)
	TextView proMyPayment;
	@BindView(R.id.pro_settle_money)
	TextView proSettleMoney;
	@BindView(R.id.project_industry)
	TextView projectIndustry;
	@BindView(R.id.project_nature)
	TextView projectNature;
	@BindView(R.id.project_module_demand)
	TextView projectModuleDemand;
	@BindView(R.id.project_post)
	TextView projectPost;
	@BindView(R.id.project_require)
	TextView projectRequire;
	@BindView(R.id.project_duty)
	TextView projectDuty;
	@BindView(R.id.project_intro)
	TextView projectIntro;
	private Intent mIntent;
	private JSONObject jb;
	private ProjectBean projectBean;
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			dismissLoadPd();
			try {
				jb = new JSONObject((String) msg.obj);
				switch (msg.what) {
					case 1:
						projectBean = new Gson().fromJson(jb.getString("result"), new TypeToken<ProjectBean>() {
						}.getType());
						initData();
						break;
					case 2:
						mIntent = new Intent(ProjectDetailActivity.this, ProjectApplyExpenseActivity.class);
						mIntent.putExtra("project_name", projectBean.getProjectInfo().getProjectName());
						mIntent.putExtra("project_id", proID);
						mIntent.putExtra("project_approver", projectBean.getApprover());
						mIntent.putExtra("expense_id", jb.getJSONObject("result").getString("no"));
						mIntent.putExtra("expense_wbs", projectBean.getCostCode().split(","));
						startActivity(mIntent);
						break;
					case 3:
						mIntent = new Intent(ProjectDetailActivity.this, ProjectApplyHourActivity.class);
						mIntent.putExtra("project_name", projectBean.getProjectInfo().getProjectName());
						mIntent.putExtra("project_id", proID);
						mIntent.putExtra("project_approver", projectBean.getApprover());
						mIntent.putExtra("project_contract", projectBean.getAgreementNO());
						mIntent.putExtra("hour_id", jb.getJSONObject("result").getString("no"));
						mIntent.putExtra("hour_wbs", projectBean.getTimeCode());
						mIntent.putExtra("hour_realSalary", projectBean.getRealSalary());
						startActivity(mIntent);
						break;
					case 99:
						ToastUtil.show(ProjectDetailActivity.this, (String) msg.obj);
						break;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	};

	@Override
	public int getContentViewId() {
		return R.layout.act_project_detail;
	}

	@Override
	public void initAllMembers(Bundle savedInstanceState) {
		super.initAllMembers(savedInstanceState);
		proID = getIntent().getStringExtra("id");
		showLoadPd();
		initView();
		requestPro();
	}

	private void initView() {
		commonTitle.setText(getResources().getString(R.string.project_info_mine));
		commonRightImg.setBackground(getResources().getDrawable(R.drawable.tittlebar_share));
		commonRightImg.setVisibility(View.GONE);
		projectDetailBottom1.setVisibility(View.GONE);
	}

	private void initData() {
		proTitle.setText(projectBean.getProjectInfo().getProjectName());
		proDemandNum.setText(getProValue(R.string.pro_no) + projectBean.getProjectInfo().getRequireNo());
		proClientName.setText(getProValue(R.string.pro_customer) + projectBean.getCustomerName());
		proName.setText(getProValue(R.string.pro_name) + projectBean.getProjectInfo().getProjectName());
		proStartTime.setText(projectBean.getProjectInfo().getStartDate() + " " + getString(R.string.pro_start));
		if (projectBean.getProjectInfo().getProjectCycle().equals("-99")) {
			proPeriod.setText(Html.fromHtml(periodHtml2.replace("0", "入职")));
		} else {
			proPeriod.setText(Html.fromHtml(periodHtml.replace("0", projectBean.getProjectInfo().getProjectCycle())));
		}
		proPosition.setText(projectBean.getProjectInfo().getProjectAddressName());
		proPayWay.setText(getProValue(R.string.pro_pay_way) + projectBean.getProjectInfo().getPayMethod());
		proWorkWay.setText(getProValue(R.string.pro_work_way) + projectBean.getProjectInfo().getImpMethod());
		String moneyStr = projectBean.getRealSalary() == 0 ? "未发布" : "" + projectBean.getRealSalary();
		proMyPayment.setText(Html.fromHtml(paymentHtml.replace("0", moneyStr)));
		moneyStr = projectBean.getAmount() == 0 ? "未发布" : "" + projectBean.getAmount();
		proSettleMoney.setText(Html.fromHtml(settleHtml.replace("0", moneyStr)));
		projectIndustry.setText(getProValue(R.string.pro_industry) + projectBean.getProjectInfo().getProjectIndustry());
		projectNature.setText(getProValue(R.string.pro_nature));//项目性质
		projectModuleDemand.setText(getProValue(R.string.pro_need) + projectBean.getProjectInfo().getModelRequire());
		projectPost.setText(getProValue(R.string.pro_position) + projectBean.getProjectInfo().getStation());
		projectRequire.setText(projectBean.getProjectInfo().getProjectRequire());
		projectDuty.setText(getProValue(R.string.pro_duty) + projectBean.getProjectInfo().getStationDuty());
		projectIntro.setText(projectBean.getProjectInfo().getProjectComment());
	}

	@OnClick({R.id.common_back, R.id.common_right_img, R.id.project_apply_expense, R.id.project_apply_manHour})
	public void onViewClicked(View view) {
		switch (view.getId()) {
			case R.id.common_back:
				finish();
				break;
			case R.id.common_right_img:
				// TODO: 2017-8-7 分享，后期加具体操作
				ToastUtil.show(this, "分享");
				break;
			case R.id.project_apply_expense:
				if (checkInfo()) {
					requestExpenseNo();
				}
				break;
			case R.id.project_apply_manHour:
				if (checkInfo()) {
					requestHourNo();
				}
				break;
		}
	}

	private boolean checkInfo() {
		if (TextUtils.isEmpty(projectBean.getCostCode())) {
			ToastUtil.show(ProjectDetailActivity.this, getResources().getString(R.string.expense_no_wbs));
			return false;
		} else if (TextUtils.isEmpty(Utils.messageInfo.userInfo.getBankType()) || TextUtils.isEmpty(Utils.messageInfo.userInfo.getBankAccount())) {
			ToastUtil.show(ProjectDetailActivity.this, getResources().getString(R.string.hour_no_bank));
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 请求项目信息
	 */
	private void requestPro() {
		if (!TextUtils.isEmpty(proID)) {
			new NetworkConnectGet(this, Api.PROJECT_DETAIL + proID, mHandler, 1);
		} else {
			ToastUtil.show(this, getResources().getString(R.string.pro_detail_error));
		}
	}

	/**
	 * 请求报销单号
	 */
	private void requestExpenseNo() {
		new NetworkConnectGet(this, Api.PROJECT_EXPENSE_NO, mHandler, 2);
	}

	/**
	 * 请求工时单号
	 */
	private void requestHourNo() {
		new NetworkConnectGet(this, Api.PROJECT_HOUR_NO, mHandler, 3);
	}

	public String getProValue(int resID) {
		return getString(resID) + " ";
	}
}
